<template>
  <view class="container">
    <view class="alert">
      <text>中国大唐集团能源投资有限公司</text>
    </view>
    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
        :border="false"
        err-show-type="toast"
      >
        <text class="title">访客登记</text>
        <br />
        <text class="intro"
          >您好，这里是中国大唐集团能源投资有限公司欠薪欠款维权通道，如您在中国大唐集团能源投资有限公司所属项目遇有农民工工资拖欠、劳务分包欠款、其他欠款等情况，请您认真填写以下信息，我们将第一时间核实处理，如不满意处理进度或结果，可直接拨打电话咨询处理情况。感谢您对中国大唐集团能源投资有限公司的信任与支持!</text
        >
        <br />
        <br />
        <uni-forms-item label="来访时间" required name="visitorTime">
          <uni-datetime-picker
            type="datetime"
            hide-second
            :start="new Date().getTime()"
            v-model="formData.visitorTime"
          />
        </uni-forms-item>
        <uni-forms-item label="姓名" required name="visitorName">
          <uni-easyinput v-model="formData.visitorName" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="联系方式" required name="visitorPhone">
          <uni-easyinput v-model="formData.visitorPhone" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="所属单位" required name="dept">
          <uni-easyinput v-model="formData.dept" placeholder="请输入" />
        </uni-forms-item>
        <uni-forms-item label="详细内容" name="visitorInterviewee">
          <uni-easyinput
            v-model="formData.visitorInterviewee"
            placeholder="请输入"
          />
        </uni-forms-item>

        <uni-popup
          ref="popup"
          borderRadius="40rpx 40rpx 0 0"
          background-color="#fff"
        >
          <view class="secondBpx">
            <view class="tip">
              <view class="n">安保人员需与受访人进行探访确认</view>
              <view class="v">受访人电话：{{ formData.intervieweePhone }}</view>
            </view>
            <uni-forms-item
              label="是否与受访人经过确认"
              required
              name="isIntervieweeConfirmed"
            >
              <uni-data-checkbox
                v-model="formData.isIntervieweeConfirmed"
                :localdata="isYesNo"
              ></uni-data-checkbox>
            </uni-forms-item>
            <uni-forms-item
              label="安保人员姓名"
              name="visitorSecurity"
              required
            >
              <uni-easyinput
                v-model="formData.visitorSecurity"
                placeholder="请输入"
              />
            </uni-forms-item>
            <button class="btn" type="primary" @click="onSubmit('valiForm')">
              提交
            </button>
          </view>
        </uni-popup>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onSubmit('valiForm')">
        提交
      </button>
    </view>
  </view>
</template>

<script>
import { getUserAllList } from '@/api/index';
import { addVisitor } from '@/api/visitor';
import { getVisitorInviteDetail } from '@/api/workOrder';
import useAppStore from '@/store/modules/app';
export default {
  components: {},
  data() {
    return {
      isYesNo: [
        {
          text: '是',
          label: '是',
          value: '1',
        },
        {
          text: '否',
          label: '否',
          value: '0',
        },
      ],
      query: {},
      isShow: false,
      // 表单数据
      formData: { visitorTime: new Date().getTime() },
      // 规则
      rules: {
        visitorTime: {
          rules: [
            {
              required: true,
              errorMessage: '请选择日期',
            },
          ],
        },
        visitorName: {
          rules: [
            {
              required: true,
              errorMessage: '姓名不能为空',
            },
          ],
        },
        visitorPhone: {
          rules: [
            {
              required: true,
              errorMessage: '请输入手机号',
            },
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              errorMessage: '请输入正确的手机号码',
            },
          ],
        },
        dept: {
          rules: [
            {
              required: true,
              errorMessage: '请输入所属单位',
            },
          ],
        },
      },
      // store
      appStore: useAppStore(),
    };
  },
  watch: {
    'formData.date': {
      handler(newVal) {},
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    this.query = query;
    // if (this.query.id) this.getData();
    // this.$util.timestampToTime(new Date().getTime())
  },
  methods: {
    // 获取数据
    async getData() {
      let personData = [];
      await getUserAllList().then((res) => {
        personData = res.data;
      });
      getVisitorInviteDetail(this.query.id).then((res) => {
        this.formData = {
          ...this.formData,
          visitWay: '2',
          visitorName: res.data.visitorName,
          visitorPhone: res.data.visitorPhone,
          visitorInterviewee: personData.filter(
            (item) => item.userId === res.data?.entourageUserId
          )[0]['nickName'],
        };
      });
    },
    // 提交
    onNext(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          // 成功返回，res 为对应表单数据
          this.$refs.popup.open('bottom');
        })
        .catch((err) => {
          // 表单校验验失败，err 为具体错误信息
        });
    },
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          uni.showToast({
            title: `提交成功`,
          });
          // 重置
          this.onReset();
          // loading提示
          // uni.showLoading({ title: '提交中' });
          // 提交接口
          // let params = {
          //   ...res,
          //   visitorTime:
          //     res.visitorTime.length === 19
          //       ? res.visitorTime
          //       : res.visitorTime + ':00',
          // };
          // addVisitor(params).then((res) => {
          //   uni.showToast({
          //     title: `提交成功`,
          //   });
          //   // 重置
          // this.$refs.popup.close();
          //   this.onReset();
          // });
        })
        .catch((err) => {
          console.log('err', err);
          this.$refs.popup.close();
        });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 0rpx 36rpx;
  padding-bottom: calc(var(--window-bottom) + 200rpx);
  display: flex;
  flex-direction: column;

  .alert {
    padding: 16rpx;
    margin: 24rpx 0;
    text-align: center;
    border-radius: 12rpx;
    text {
      font-size: 32rpx;
      font-weight: 600;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;
    .title {
      color: #326cc1;
      font-size: 50rpx;
      line-height: 100rpx;
      font-weight: 600;
      letter-spacing: 3rpx;
    }
    .intro {
      color: #777;
      line-height: 40rpx;
    }
    ::v-deep .uni-forms-item__label {
      width: 50%;
      color: #333333;
      font-size: 28rpx;
      font-weight: bold;
      position: relative;

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .secondBpx {
    padding: 40rpx;
    .tip {
      .n {
        font-size: 38rpx;
        font-weight: 600;
      }
      .v {
        color: #989898;
        padding: 10rpx 0 15rpx;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36rpx;
    right: 36rpx;
    bottom: calc(var(--window-bottom) + 60rpx);
    .btn {
      width: 100%;
      height: 92rpx;
      font-size: 40rpx;
      color: #ffffff;
      line-height: 92rpx;
      background: #3681ff;
    }
  }
}
</style>
